import { _decorator, Button, Component, Label, Node, ProgressBar, sp, tween, UIOpacity, Vec3 } from 'cc';
import { FortressBase } from './FortressBase';
import { MapManager } from '../game/MapManager';
import { GameDefine } from '../game/GameDefine';
import { CameraManager } from '../game/CameraManager';
import { EnemyManger } from '../actor/EnemyManger';
import { AudioManager } from '../game/AudioManager';
import { Constants, FortressConfigs, Orientation } from '../game/Constants';
import { EventListener } from '../core/EventListener';
import { UIManager } from '../gui/UIManager';
const { ccclass, property } = _decorator;

/** 移动要塞管理器 */
@ccclass('FortressManager')
export class FortressManager extends Component {
    public static Instance: FortressManager = null;
    // 要塞1-4
    @property(FortressBase) Fortress1: FortressBase = null!;
    @property(FortressBase) Fortress2: FortressBase = null!;
    @property(FortressBase) Fortress3: FortressBase = null!;
    @property(FortressBase) Fortress4: FortressBase = null!;

    // 前景特效
    @property(Node) levelUpEffectBefore: Node = null!;
    @property(Node) levelUpEffectAfter: Node = null!;
    @property(Button) btnLevelUp: Button = null!;

    private _beforOpacity: UIOpacity = null!;
    private _afterOpacity: UIOpacity = null!;

    onLoad() {
        FortressManager.Instance = this;
    }

    start() {
        this._beforOpacity = this.levelUpEffectBefore.getComponent(UIOpacity);
        this._afterOpacity = this.levelUpEffectAfter.getComponent(UIOpacity);
        this.btnLevelUp.node.on(Button.EventType.CLICK, () => {
            this.onLevelUp(null);
        }, this);
        // 初始化要塞状态
        this.Fortress1.isActived = true;
        this.Fortress2.isActived = false;
        this.Fortress3.isActived = false;
        this.Fortress4.isActived = false;

        this.initOrientationUI();
        EventListener.on(Constants.Event_OrientationChange, this.initOrientationUI, this);
    }

    private initOrientationUI() {
        if (UIManager.Instance.orientation === Orientation.Landscape) {
            this.node.setPosition(new Vec3(-260, -270, 0));
            this.node.scale = new Vec3(1, 1, 1);
        } else {
            this.node.setPosition(new Vec3(-200, -270, 0));
            this.node.scale = new Vec3(0.9, 0.9, 1);
        }
    }

    /** 玩家点击手动攻击 */
    public onPlayFire() {
        this.getFortress(GameDefine.FortressLevel).onPlayFireAnim();
    }

    /** 要塞停止移动 */
    public stopMove() {
        const targetFortess = this.getFortress(GameDefine.FortressLevel);
        tween(targetFortess.anim)
            .to(3, { timeScale: 0 })
            .start();
        tween({ value: MapManager.Instance.moveSpeed })
            .to(3, { value: 0 })
            .call((target) => {
                MapManager.Instance.moveSpeed = target.value;
            })
            .start();
    }

    /** 获取当前要塞 */
    private getFortress(level: number): FortressBase {
        return level === 1 ? this.Fortress1 : level === 2 ? this.Fortress2 : level === 3 ? this.Fortress3 : this.Fortress4;
    }

    /** 要塞升级 */
    public onLevelUp(cb: Function = null) {
        if (GameDefine.FortressLevel >= 4) {
            console.log('要塞已达最高等级');
            return;
        }

        AudioManager.Instance.playLevelUp();
        let curOp = this.getFortress(GameDefine.FortressLevel).getComponent(UIOpacity);
        let nextOp = this.getFortress(GameDefine.FortressLevel + 1).getComponent(UIOpacity);
        const focusTime = 0.5;
        const t1 = tween(this._beforOpacity).to(0.5, { opacity: 255 }).delay(1).to(0.5, { opacity: 0 });
        const t2 = tween(this._afterOpacity).to(0.5, { opacity: 255 }).delay(1).to(0.5, { opacity: 0 });
        const t3 = tween(curOp).to(0.5, { opacity: 0 })
            .call(() => {
                curOp.node.active = false;
            });
        const t4 = tween(nextOp).to(0.5, { opacity: 255 });

        MapManager.Instance.changeMap();
        tween(this.node)
            .delay(focusTime)
            .parallel(t1, t2, t3, t4)
            .call(() => {
                // 升级完成
                GameDefine.FortressLevel += 1;
                // 开始播放动画
                const curFortress = this.getFortress(GameDefine.FortressLevel);
                curFortress.isActived = true;
                curFortress.anim.timeScale = 1;
                if (GameDefine.FortressLevel === 4) {
                    EventListener.emit('start_boss_fight');
                }
                EnemyManger.Instance.autoCreateEnemy(GameDefine.FortressLevel);
                cb?.();
            })
            .start();
    }
}


